#include <netinet/in.h>
#include <sys/socket.h>
#include <fcntl.h>

#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main(int argc, char **argv)
{
    int fd;
    struct sockaddr_in sin;

    bzero(&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_port = htons(12345);

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0) return -1;

#ifndef WIN32
    {
        int one = 1;
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    }
#endif

    if (connect(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        perror("connect");
        return 0;
    }

    char *buf_s = "hello water-world!\n";
    int n_write = send(fd, buf_s, strlen(buf_s), 0);
    if (n_write < 0) {
        perror("send error\n");
        return 0;
    }

    char buf_r[1000] = {0};
    int n_recv = recv(fd, buf_r, sizeof(buf_r), 0);
    if (n_recv < 0) {
        perror("recv");
        close(fd);
        return -1;
    }
    printf("%s\n", buf_r);
    close(fd);
    return 0;
}
